package org.example._04_TankWar;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.*;


public class TankClient extends Frame {
    /**
     * 这是主窗口的宽度。
     */
    public static final int GAME_WIDTH = 800;
    /**
     * 这是主窗口的高度。
     */
    public static final int GAME_HEIGHT = 600;

    public static final KeyAdapter MyYank = null;
    Tank MyTank = new Tank(600, 800, true, Tank.Direction.STOP, this);
//	Tank enemyTank = new Tank(100,100,false,this);
//	Explode e = new Explode(70,70,this);
    /**
     * new 两个墙。指定他们出现的位置。
     */
//    Wall w1 =new Wall(90,200,400,20,this);
//    Wall w2 =new Wall(500,50,20,200,this);

    /**
     * 创建爆炸，tank，生命值的集合。
     */
    java.util.List<Explode> Explodes = new ArrayList<Explode>();
    java.util.List<Missile> missiles = new ArrayList<Missile>();
    List<Tank> Tanks = new ArrayList<Tank>();

    Blood b = new Blood();

    Image offScreenImage = null;

    /**
     * 这是主窗口的画法。
     */
    public void paint(Graphics g) {
        /**
         * 在主窗口中画出子弹，爆炸，坦克的个数，
         * 以及坦克的生命值。
         */
        g.drawString("missiles count" + missiles.size(), 10, 50);
        g.drawString("Explodes count" + Explodes.size(), 10, 70);
        g.drawString("Tanks count" + Tanks.size(), 10, 90);
        g.drawString("Tanks life" + MyTank.getLife(), 10, 100);

        /**
         * TANK被全部杀死再生成10台tank。
         */
        if (Tanks.size() <= 0) {
            for (int x = 0; x < 50; x++) {
                Tanks.add(new Tank(50 + 40 * (x + 1), 50, false, Tank.Direction.D, this));
            }
        }

        /**
         * 定义子弹循环，
         */
        for (int x = 0; x < missiles.size(); x++) {
            Missile m = missiles.get(x);
//			m.HitTank(enemyTank);
            m.HitTanks(Tanks);
            m.HitTank(MyTank);
//            m.Hitwall(w1);
//            m.Hitwall(w2);
            m.draw(g);

//			if(!m.isLive()) missiles.remove(m);
//			else m.draw(g);
        }
        /**
         * 定义爆炸的循环。
         */
        for (int x = 0; x < Explodes.size(); x++) {
            Explode e = Explodes.get(x);
            e.draw(g);
        }
        for (int x = 0; x < Tanks.size(); x++) {
            Tank t = Tanks.get(x);
//            t.collidesWall(w1);
//            t.collidesWall(w2);
            t.HitTanks(Tanks);
            t.draw(g);
        }
        MyTank.draw(g);
        MyTank.eat(b);
//		enemyTank.draw(g);
//		y=y+10;
//        w1.draw(g);
//        w2.draw(g);
        b.draw(g);
    }


    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics gOffscreen = offScreenImage.getGraphics();
        Color c = gOffscreen.getColor();
        gOffscreen.setColor(Color.LIGHT_GRAY);
        gOffscreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        gOffscreen.setColor(c);
        paint(gOffscreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    public void lauchFrame() {
        for (int x = 0; x < 10; x++) {
            Tanks.add(new Tank(50 + 40 * (x + 1), 50, false, Tank.Direction.D, this));
        }
        setBounds(200, 100, GAME_WIDTH, GAME_HEIGHT);
        this.addWindowListener(new WindowAdapter() {
                                   public void windowClosing(WindowEvent e) {
                                       System.exit(0);
                                   }
                               }
        );
        this.setTitle("坦克大战");
        setBackground(Color.GREEN);
        setResizable(false);

        this.addKeyListener(new KeyMoniter());

        setVisible(true);

        new Thread(new PaintThread()).start();
    }


    public static void main(String[] args) {
        TankClient tc = new TankClient();
        tc.lauchFrame();
    }

    private class PaintThread implements Runnable {
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                repaint();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }


    }

    private class KeyMoniter extends KeyAdapter {
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub
            MyTank.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            MyTank.keyPressed(e);
        }

    }
}

